package common.wx;

import org.apache.commons.lang.StringUtils;

import common.impl.MicroBlog;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weibo4j.model.PostParameter;

public class WxAuth {

	public static final String SCOPE_BASE = "snsapi_base";
	public static final String SCOPE_USERINFO = "snsapi_userinfo";
	public static final int AUTH_TYPE_BASE = 0;
	public static final int AUTH_TYPE_USERINFO = 1;

	private static final String authUrl_start = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe167101d186c5ec4&redirect_uri=";
	private static final String authUrl_start2 = "&response_type=code&scope=";
	private static final String authUrl_center = "&state=";
	private static final String authUrl_end = "#wechat_redirect";
	public final static String APPID = "wxe167101d186c5ec4";
	public final static String APPSECRET = "864ea70e2d3c3ff8c6f494407008261e";
	private final static String GRANT_TYPE = "authorization_code";
	private final static String URL_GETOPENID = "https://api.weixin.qq.com/sns/oauth2/access_token";
	private final static String URL_GETUSERINFO = "https://api.weixin.qq.com/sns/userinfo";

	// 是否关注
	public final static String url_req = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";
	public final static String url_twoCode_ticket = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN";
	public final static String url_req1 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

	/**
	 * 获取base授权Url
	 * 
	 * @param state
	 * @return
	 */
	public static String getAuthUrl(int AUTH_TYPE, String state, String redirectUri) {
		if (AUTH_TYPE == AUTH_TYPE_BASE) {
			return new StringBuffer().append(authUrl_start).append(redirectUri).append(authUrl_start2)
					.append(SCOPE_BASE).append(authUrl_center).append(state).append(authUrl_end).toString();
		} else if (AUTH_TYPE == AUTH_TYPE_USERINFO) {
			return new StringBuffer().append(authUrl_start).append(redirectUri).append(authUrl_start2)
					.append(SCOPE_USERINFO).append(authUrl_center).append(state).append(authUrl_end).toString();
		} else {
			return null;
		}
	}

	/**
	 * 批量获取用户信息
	 * 
	 * @param openIds
	 * @return
	 * @throws Exception
	 */
	public static String getUserInfo(String openId) throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja_user_list = new JSONArray();
		JSONObject jo_temp = new JSONObject();
		jo_temp.put("openid", openId);
		jo_temp.put("lang", "zh-CN");
		ja_user_list.add(jo_temp);
		jo.put("user_list", ja_user_list);
		String accessToken = WxAccessToken.getAccessToken();
		String url = url_req.replaceAll("ACCESS_TOKEN", accessToken);
		return MicroBlog.post(url, jo.toString(), null);
	}

	public static WxUser getByCode(String code) {
		String r1 = MicroBlog.post(URL_GETOPENID,
				new PostParameter[] { new PostParameter("appid", APPID), new PostParameter("secret", APPSECRET),
						new PostParameter("code", code), new PostParameter("grant_type", GRANT_TYPE) });
		WxUser wxUser = null;
		try {
			JSONObject jobj1 = JSONObject.fromObject(r1);
			String openId = jobj1.optString("openid");
			wxUser = new WxUser();
			wxUser.setOpenId(openId);
			String scope = jobj1.optString("scope");
			if ("snsapi_userinfo".equals(scope)) {
				String accessToken = jobj1.optString("access_token");
				if (!StringUtils.isEmpty(accessToken)) {
					// 从微信服务器获取信息
					String r = MicroBlog.post(URL_GETUSERINFO,
							new PostParameter[] { new PostParameter("access_token", accessToken),
									new PostParameter("openid", openId), new PostParameter("lang", "zh_CN") });
					JSONObject jobj = JSONObject.fromObject(r);
					if (jobj.containsKey("nickname") && jobj.containsKey("headimgurl")) {
						wxUser.setNickName(jobj.optString("nickname"));
						wxUser.setHeadUrl(jobj.optString("headimgurl"));
						wxUser.setAuth(true);
					}
				}
			}
		} catch (Exception e) {
		}
		return wxUser;
	}

	public static class WxUser {

		private String openId;
		private String nickName;
		private String headUrl;
		private boolean isAuth;

		public String getOpenId() {
			return openId;
		}

		public void setOpenId(String openId) {
			this.openId = openId;
		}

		public String getNickName() {
			return nickName;
		}

		public void setNickName(String nickName) {
			this.nickName = nickName;
		}

		public String getHeadUrl() {
			return headUrl;
		}

		public void setHeadUrl(String headUrl) {
			this.headUrl = headUrl;
		}

		public boolean isAuth() {
			return isAuth;
		}

		public void setAuth(boolean isAuth) {
			this.isAuth = isAuth;
		}
	}

}
